home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / lisp / modes / hideif.el < prev    next >
Encoding:
Text File  |  1995-05-12  |  31.5 KB  |  1,040 lines

  1. ;;; hide-ifdef-mode.el --- hides selected code within ifdef.
  2. ;; Keywords: c
  3.  
  4. ;;; Copyright (C) 1988 Brian Marick and Daniel LaLiberte
  5. ;;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
  6. ;;; Extensively modified by Daniel LaLiberte (while at Gould).
  7. ;;;
  8. ;;; You may freely modify and distribute this, but keep a record
  9. ;;; of modifications and send comments to:
  10. ;;;      liberte@a.cs.uiuc.edu  or  ihnp4!uiucdcs!liberte
  11. ;;; I will continue to upgrade hide-ifdef-mode
  12. ;;; with your contributions and will eventually offer it to FSF.
  13.  
  14. ;;; Revision 1.7  88/02/16  03:12:58  liberte
  15. ;;; Fixed comments and doc strings.
  16. ;;; Added optional prefix arg for ifdef motion commands.
  17. ;;; 
  18. ;;; Revision 1.6  88/02/05  00:36:18  liberte
  19. ;;; Bug fixes.
  20. ;;; 1. A multi-line comment that starts on an #ifdef line
  21. ;;;    now ends on that line.
  22. ;;; 2. Fix bad function name: hide-hif-ifdef-toggle-read-only
  23. ;;; 3. Make ifdef-block hiding work outside of ifdefs.
  24. ;;; 
  25. ;;; Revision 1.5  88/01/31  23:19:31  liberte
  26. ;;; Major clean up.
  27. ;;;   Prefix internal names with "hif-".
  28. ;;; 
  29. ;;; Revision 1.4  88/01/30  14:09:38  liberte
  30. ;;; Add hide-ifdef-hiding and hide-ifdef-mode to minor-mode-alist.
  31. ;;; 
  32. ;;; Revision 1.3  88/01/29  00:38:19  liberte
  33. ;;; Fix three bugs.
  34. ;;; 1. Function "defined" is just like lookup.
  35. ;;; 2. Skip to newline or cr in case text is hidden.
  36. ;;; 3. Use car of token list if just one symbol.
  37. ;;;
  38. ;;; Revision 1.2  88/01/28  23:32:46  liberte
  39. ;;; Use hide-ifdef-mode-prefix-key.
  40. ;;; Copy current-local-map so other buffers do not get
  41. ;;; hide-ifdef-mode bindings.
  42. ;;;
  43. ;;;--------------------------------------------------------------
  44. ;;; To initialize, toggle the hide-ifdef minor mode with
  45. ;;;
  46. ;;; M-x hide-ifdef-mode
  47. ;;;
  48. ;;; This will set up key bindings and call hide-ifdef-mode-hook if it
  49. ;;; has a value.  To explicitly hide ifdefs using a buffer-local
  50. ;;; define list (default empty), type
  51. ;;;
  52. ;;; M-x hide-ifdefs  or C-c h
  53. ;;;
  54. ;;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
  55. ;;; pass through.  The support of constant expressions in #if lines is 
  56. ;;; limited to identifiers, parens, and the operators: &&, ||, !, and
  57. ;;; "defined".  Please extend this.
  58. ;;;
  59. ;;; The hidden code is marked by ellipses (...).  Be
  60. ;;; cautious when editing near ellipses, since the hidden text is
  61. ;;; still in the buffer, and you can move the point into it and modify
  62. ;;; text unawares.  If you don't want to see the ellipses, set 
  63. ;;; selective-display-ellipses to nil.  But this can be dangerous.
  64. ;;; You can make your buffer read-only while hide-ifdef-hiding by setting
  65. ;;; hide-ifdef-read-only to a non-nil value.  You can toggle this 
  66. ;;; variable with hide-ifdef-toggle-read-only (C-c C-q).
  67. ;;;
  68. ;;; You can undo the effect of hide-ifdefs by typing
  69. ;;;
  70. ;;; M-x show-ifdefs  or C-c s
  71. ;;;
  72. ;;; Use M-x hide-ifdef-define (C-c d) to define a symbol.
  73. ;;; Use M-x hide-ifdef-undef (C-c u) to undefine a symbol.
  74. ;;;
  75. ;;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
  76. ;;; the display will be updated.  Only the define list for the current
  77. ;;; buffer will be affected.  You can save changes to the local define
  78. ;;; list with hide-ifdef-set-define-alist.  This adds entries 
  79. ;;; to hide-ifdef-define-alist.
  80. ;;;
  81. ;;; If you have defined a hide-ifdef-mode-hook, you can set
  82. ;;; up a list of symbols that may be used by hide-ifdefs as in the
  83. ;;; following example:
  84. ;;;
  85. ;;; (setq hide-ifdef-mode-hook
  86. ;;;      '(lambda ()
  87. ;;;     (if (not hide-ifdef-define-alist)
  88. ;;;         (setq hide-ifdef-define-alist
  89. ;;;          '((list1 ONE TWO)
  90. ;;;            (list2 TWO THREE)
  91. ;;;            )))
  92. ;;;     (hide-ifdef-use-define-alist 'list2) ; use list2 by default
  93. ;;;     ))
  94. ;;;
  95. ;;; You can call hide-ifdef-use-define-alist (C-c u) at any time to specify
  96. ;;; another list to use.
  97. ;;;
  98. ;;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
  99. ;;; set hide-ifdef-initially to non-nil.
  100. ;;;
  101. ;;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
  102. ;;; In the absence of highlighting, that might be a bad idea.  If you set
  103. ;;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
  104. ;;; lines will be displayed.  That can be confusing in its own
  105. ;;; right.  Other variations on display are possible, but not much
  106. ;;; better.
  107. ;;;
  108. ;;; You can explicitly hide or show individual ifdef blocks irrespective
  109. ;;; of the define list by using hide-ifdef-block and show-ifdef-block.
  110. ;;;
  111. ;;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
  112. ;;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
  113. ;;;
  114. ;;; If you have minor-mode-alist in your modeline (the default) two labels
  115. ;;; may appear.  "Ifdef" will appear when hide-ifdef-mode is active.  "Hiding"
  116. ;;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
  117.  
  118.  
  119.  
  120. (defvar hide-ifdef-mode-map nil
  121.   "Keymap used with hide-ifdef mode")
  122.  
  123. (defconst hide-ifdef-mode-prefix-key "\C-c"
  124.   "Prefix key for all hide-ifdef-mode commands.")
  125.  
  126. (defvar hide-ifdef-mode-map-before nil
  127.   "Buffer-local variable to store a copy of the local keymap
  128.     before hide-ifdef-mode modifies it.")
  129.  
  130. (defun define-hide-ifdef-mode-map ()
  131.   (if hide-ifdef-mode-map
  132.       ()                ; dont redefine it.
  133.     (setq hide-ifdef-mode-map (make-sparse-keymap))
  134.     (define-key hide-ifdef-mode-map "d" 'hide-ifdef-define)
  135.     (define-key hide-ifdef-mode-map "u" 'hide-ifdef-undef)
  136.     (define-key hide-ifdef-mode-map "D" 'hide-ifdef-set-define-alist)
  137.     (define-key hide-ifdef-mode-map "U" 'hide-ifdef-use-define-alist)
  138.   
  139.     (define-key hide-ifdef-mode-map "h" 'hide-ifdefs)
  140.     (define-key hide-ifdef-mode-map "s" 'show-ifdefs)
  141.     (define-key hide-ifdef-mode-map "\C-h" 'hide-ifdef-block)
  142.     (define-key hide-ifdef-mode-map "\C-s" 'show-ifdef-block)
  143.   
  144.     (define-key hide-ifdef-mode-map "\C-f" 'forward-ifdef)
  145.     (define-key hide-ifdef-mode-map "\C-b" 'backward-ifdef)
  146.     (define-key hide-ifdef-mode-map "\C-d" 'down-ifdef)
  147.     (define-key hide-ifdef-mode-map "\C-u" 'up-ifdef)
  148.     (define-key hide-ifdef-mode-map "\C-n" 'next-ifdef)
  149.     (define-key hide-ifdef-mode-map "\C-p" 'previous-ifdef)
  150.     (define-key hide-ifdef-mode-map "\C-q" 'hide-ifdef-toggle-read-only)
  151.     (define-key hide-ifdef-mode-map
  152.       (where-is-internal 'toggle-read-only nil t)
  153.       'hide-ifdef-toggle-outside-read-only)
  154.     )
  155.   (fset 'hide-ifdef-mode-map hide-ifdef-mode-map)  ; the function is the map
  156.   )
  157.  
  158. (defun hif-update-mode-line ()
  159.   "Update mode-line by setting buffer-modified to itself."
  160.   (set-buffer-modified-p (buffer-modified-p)))
  161.  
  162.  
  163. (defvar hide-ifdef-mode nil
  164.   "non-nil when hide-ifdef-mode is activated.")
  165.  
  166. (defvar hide-ifdef-hiding nil
  167.   "non-nil when text may be hidden.")
  168.  
  169. (or (assq 'hide-ifdef-hiding minor-mode-alist)
  170.     (setq minor-mode-alist
  171.           (cons '(hide-ifdef-hiding " Hiding")
  172.                 minor-mode-alist)))
  173.  
  174. (or (assq 'hide-ifdef-mode minor-mode-alist)
  175.     (setq minor-mode-alist
  176.           (cons '(hide-ifdef-mode " Ifdef")
  177.                 minor-mode-alist)))
  178.  
  179.  
  180. (defun hide-ifdef-mode (arg)
  181.   "Toggle hide-ifdef-mode.  Thus this is a minor mode, albeit a large one.
  182. With arg, turn hide-ifdef-mode on iff arg is positive.
  183. In hide-ifdef-mode, code within #ifdef constructs that the C preprocessor
  184. would eliminate may be hidden from view.  Several variables affect
  185. how the hiding is done:
  186.  
  187. hide-ifdef-env
  188.     An association list of defined and undefined symbols for the
  189.     current buffer.  Initially, the global value of hide-ifdef-env is used.
  190.  
  191. hide-ifdef-define-alist
  192.     An association list of defined symbol lists.  
  193.         Use hide-ifdef-set-define-alist to save the current hide-ifdef-env
  194.         and hide-ifdef-use-define-alist to set the current hide-ifdef-env
  195.         from one of the lists in hide-ifdef-define-alist.
  196.  
  197. hide-ifdef-lines
  198.     Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
  199.     #endif lines when hiding.
  200.  
  201. hide-ifdef-initially
  202.     Indicates whether hide-ifdefs should be called when hide-ifdef-mode
  203.     is activated.
  204.  
  205. hide-ifdef-read-only
  206.     Set to non-nil if you want to make buffers read only while hiding.
  207.     After show-ifdefs, read-only status is restored to previous value.
  208.  
  209. \\{hide-ifdef-mode-map}"
  210.  
  211.   (interactive "P")
  212.   (make-local-variable 'hide-ifdef-mode)
  213.   (setq hide-ifdef-mode
  214.     (if (null arg)
  215.         (not hide-ifdef-mode)
  216.       (> (prefix-numeric-value arg) 0)))
  217.   
  218.   (hif-update-mode-line)
  219.  
  220.   (if hide-ifdef-mode
  221.       (progn
  222.     ; fix c-mode syntax table so we can recognize whole symbols.
  223.     (modify-syntax-entry ?_ "w")
  224.     (modify-syntax-entry ?& ".")
  225.     (modify-syntax-entry ?\| ".")
  226.  
  227.     ; inherit global values
  228.     (make-local-variable 'hide-ifdef-env)
  229.     (setq hide-ifdef-env (default-value 'hide-ifdef-env))
  230.  
  231.     (make-local-variable 'hide-ifdef-hiding)
  232.     (setq hide-ifdef-hiding (default-value 'hide-ifdef-hiding))
  233.  
  234.     (make-local-variable 'hif-outside-read-only)
  235.     (setq hif-outside-read-only buffer-read-only)
  236.  
  237.     (make-local-variable 'hide-ifdef-mode-map-before)
  238.     (setq hide-ifdef-mode-map-before (current-local-map))
  239.     (use-local-map (copy-keymap (current-local-map)))
  240.     (local-unset-key hide-ifdef-mode-prefix-key)
  241.     (local-set-key hide-ifdef-mode-prefix-key 'hide-ifdef-mode-map)
  242.     (define-hide-ifdef-mode-map)
  243.  
  244.     (run-hooks 'hide-ifdef-mode-hook)
  245.  
  246.     (if hide-ifdef-initially
  247.         (hide-ifdefs)
  248.       (show-ifdefs))
  249.     (message "Enter hide-ifdef-mode.")
  250.     )
  251.      ; else end hide-ifdef-mode
  252.     (if hide-ifdef-hiding
  253.     (show-ifdefs))
  254.     (use-local-map hide-ifdef-mode-map-before)
  255.     (message "Exit hide-ifdef-mode.")
  256.     ))
  257.   
  258.  
  259. ;; from outline.el with docstring fixed.
  260. (defun hif-outline-flag-region (from to flag)
  261.   "Hides or shows lines from FROM to TO, according to FLAG.  If FLAG
  262. is \\n (newline character) then text is shown, while if FLAG is \\^M
  263. \(control-M) the text is hidden."
  264.   (let ((modp (buffer-modified-p)))
  265.     (unwind-protect (progn
  266.               (subst-char-in-region from to
  267.                   (if (= flag ?\n) ?\^M ?\n)
  268.                   flag t) )
  269.       (set-buffer-modified-p modp))
  270.     ))
  271.  
  272. (defun hif-show-all ()
  273.   "Show all of the text in the current buffer."
  274.   (interactive)
  275.   (hif-outline-flag-region (point-min) (point-max) ?\n))
  276.  
  277. (defun hide-ifdef-region (start end)
  278.   "START is the start of a #if or #else form.  END is the ending part.
  279. Everything including these lines is made invisible."
  280.   (hif-outline-flag-region start end ?\^M)
  281.   )
  282.  
  283. (defun hif-show-ifdef-region (start end)
  284.   "Everything between START and END is made visible."
  285.   (hif-outline-flag-region start end ?\n)
  286.   )
  287.  
  288.  
  289.  
  290. ;===%%SF%% evaluation (Start)  ===
  291.  
  292. (defvar hide-ifdef-evaluator 'eval
  293.   "The evaluator is given a canonical form and returns T if text under
  294. that form should be displayed.")
  295.  
  296. (defvar hif-undefined-symbol nil
  297.   "...is by default considered to be false.")
  298.  
  299. (defvar hide-ifdef-env nil
  300.   "An alist of defined symbols and their values.")
  301.  
  302.  
  303. (defun hif-set-var (var value)
  304.   "Prepend (var value) pair to hide-ifdef-env."
  305.   (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env)))
  306.  
  307.  
  308. (defun hif-lookup (var)
  309. ;  (message "hif-lookup %s" var)
  310.   (let ((val (assoc var hide-ifdef-env)))
  311.     (if val
  312.     (cdr val)
  313.       hif-undefined-symbol)))
  314.  
  315. (defun hif-defined (var)
  316.   (hif-lookup var)
  317.   ; when #if expressions are fully supported, defined result should be 1
  318.   ;  (if (assoc var  hide-ifdef-env)
  319.   ;      1
  320.   ;    nil)
  321. )
  322.  
  323.  
  324. ;===%%SF%% evaluation (End)  ===
  325.  
  326.  
  327.  
  328. ;===%%SF%% parsing (Start)  ===
  329. ;;;  The code that understands what ifs and ifdef in files look like.
  330.  
  331. (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*")
  332. (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef"))
  333. (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+"))
  334. (defconst hif-else-regexp (concat hif-cpp-prefix "else"))
  335. (defconst hif-endif-regexp (concat hif-cpp-prefix "endif"))
  336. (defconst hif-ifx-else-endif-regexp
  337.   (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
  338.  
  339.  
  340. (defun hif-infix-to-prefix (token-list)
  341.   "Convert list of tokens in infix into prefix list"
  342. ;  (message "hif-infix-to-prefix: %s" token-list)
  343.   (if (= 1 (length token-list))
  344.       (` (hif-lookup (quote (, (car token-list)))))
  345.     (hif-parse-if-exp token-list))
  346.   )
  347.  
  348. ; pattern to match initial identifier, !, &&, ||, (, or ).
  349. (defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[()]\\|\\w+\\)")
  350. (defconst hif-end-of-comment "\\*/")
  351.  
  352.  
  353. (defun hif-tokenize (expr-string)
  354.   "Separate string into a list of tokens"
  355.   (let ((token-list nil)
  356.     (expr-start 0)
  357.     (expr-length (length expr-string)))
  358.  
  359.     (while (< expr-start expr-length) 
  360. ;      (message "expr-start = %d" expr-start) (sit-for 1)
  361.       (cond
  362.     ((string-match "^[ \t]+" expr-string expr-start)
  363.        ; skip whitespace
  364.      (setq expr-start (match-end 0))
  365.      ; stick newline in string so ^ matches on the next string-match
  366.      (aset expr-string (1- expr-start) ?\n)
  367.      )
  368.  
  369.     ((string-match "^/\\*" expr-string expr-start)
  370.      (setq expr-start (match-end 0))
  371.      (aset expr-string (1- expr-start) ?\n)
  372.      (or
  373.        (string-match hif-end-of-comment
  374.              expr-string expr-start) ; eat comment
  375.        (string-match "$" expr-string expr-start)) ; multi-line comment
  376.      (setq expr-start (match-end 0))
  377.      (aset expr-string (1- expr-start) ?\n)
  378.      )
  379.  
  380.     ((string-match hif-token-regexp expr-string expr-start)
  381.       (let ((token (substring expr-string expr-start (match-end 0))))
  382.         (setq expr-start (match-end 0))
  383.         (aset expr-string (1- expr-start) ?\n)
  384. ;        (message "token: %s" token) (sit-for 1)
  385.         (setq token-list
  386.           (cons
  387.             (cond
  388.               ((string-equal token "||") 'or)
  389.               ((string-equal token "&&") 'and)
  390.               ((string-equal token "!")  'not)
  391.               ((string-equal token "defined") 'hif-defined)
  392.               ((string-equal token "(") 'lparen)
  393.               ((string-equal token ")") 'rparen)
  394.               (t (intern token)))
  395.             token-list))
  396.         ))
  397.       (t (error "Bad #if expression: %s" expr-string))
  398.       ))
  399.     (nreverse token-list)
  400.     ))
  401.  
  402. ;;;-----------------------------------------------------------------
  403. ;;; Translate C preprocessor #if expressions using recursive descent.
  404. ;;; This parser is limited to the operators &&, ||, !, and "defined".
  405.  
  406. (defun hif-parse-if-exp (token-list)
  407.   "Parse the TOKEN-LIST.  Return translated list in prefix form."
  408.   (hif-nexttoken)
  409.   (prog1
  410.       (hif-expr)
  411.     (if token ; is there still a token?
  412.     (error "Error: unexpected token: %s" token)))
  413.   )
  414.  
  415. (defun hif-nexttoken ()
  416.   "Pop the next token from token-list into the let variable \"token\"."
  417.   (setq token (car token-list))
  418.   (setq token-list (cdr token-list))
  419.   token
  420.   )
  421.  
  422. (defun hif-expr ()
  423.   "Parse and expression of the form
  424.        expr : term | expr '||' term."
  425.   (let ((result (hif-term)))
  426.     (while (eq  token 'or)
  427.       (hif-nexttoken)
  428.       (setq result (list 'or result (hif-term))))
  429.   result
  430.   ))
  431.  
  432. (defun hif-term ()
  433.   "Parse a term of the form
  434.        term : factor | term '&&' factor."
  435.   (let ((result (hif-factor)))
  436.     (while (eq token 'and)
  437.       (hif-nexttoken)
  438.       (setq result (list 'and result (hif-factor))))
  439.     result
  440.     ))
  441.  
  442. (defun hif-factor ()
  443.   "Parse a factor of the form
  444.        factor : '!' factor | '(' expr ')' | 'defined(' id ')' | id."
  445.   (cond
  446.     ((eq token 'not)
  447.      (hif-nexttoken)
  448.      (list 'not (hif-factor)))
  449.  
  450.     ((eq token 'lparen)
  451.      (hif-nexttoken)
  452.      (let ((result (hif-expr)))
  453.        (if (not (eq token 'rparen))
  454.        (error "Bad token in parenthesized expression: %s" token)
  455.      (hif-nexttoken)
  456.      result)))
  457.  
  458.     ((eq token 'hif-defined)
  459.      (hif-nexttoken)
  460.      (if (not (eq token 'lparen))
  461.      (error "Error: expected \"(\" after \"defined\""))
  462.      (hif-nexttoken)
  463.      (let ((ident token))
  464.        (if (memq token '(or and not hif-defined lparen rparen))
  465.        (error "Error: unexpected token: %s" token))
  466.        (hif-nexttoken)
  467.        (if (not (eq token 'rparen))
  468.        (error "Error: expected \")\" after identifier"))
  469.        (hif-nexttoken)
  470.        (` (hif-defined (quote (, ident))))
  471.        ))
  472.  
  473.     (t ; identifier
  474.       (let ((ident token))
  475.     (if (memq ident '(or and))
  476.         (error "Error: missing identifier"))
  477.     (hif-nexttoken)
  478.     (` (hif-lookup (quote (, ident))))
  479.     ))
  480.  
  481.     ))
  482.  
  483. ;;;----------- end of parser -----------------------
  484.  
  485.  
  486. (defun hif-canonicalize ()
  487.   "When at beginning of #ifX, returns a canonical (evaluatable)
  488.        form for the expression."
  489.   (save-excursion
  490.     (let ((negate (looking-at hif-ifndef-regexp)))
  491.       (re-search-forward hif-ifx-regexp)
  492.       (let* ((expr-string
  493.           (buffer-substring (point)
  494.                 (progn (skip-chars-forward "^\n\r") (point))))
  495.          (expr (hif-infix-to-prefix (hif-tokenize expr-string))))
  496. ;    (message "hif-canonicalized: %s" expr)
  497.     (if negate
  498.         (list 'not expr)
  499.       expr)))))
  500.  
  501.  
  502. (defun hif-find-any-ifX ()
  503.   "Position at beginning of next #if, #ifdef, or #ifndef, including one on
  504. this line."
  505. ;  (message "find ifX at %d" (point))
  506.   (prog1
  507.       (re-search-forward hif-ifx-regexp (point-max) t)
  508.     (beginning-of-line)))
  509.  
  510.  
  511. (defun hif-find-next-relevant ()
  512.   "Position at beginning of next #ifdef, #ifndef, #else, #endif,
  513. NOT including one on this line."
  514. ;  (message "hif-find-next-relevant at %d" (point))
  515.   (end-of-line)
  516.   ; avoid infinite recursion by only going to beginning of line if match found
  517.   (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t)
  518.       (beginning-of-line))
  519.   )
  520.  
  521. (defun hif-find-previous-relevant ()
  522.   "Position at beginning of previous #ifdef, #ifndef, #else, #endif,
  523. NOT including one on this line."
  524. ;  (message "hif-find-previous-relevant at %d" (point))
  525.   (beginning-of-line)
  526.   ; avoid infinite recursion by only going to beginning of line if match found
  527.   (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t)
  528.      (beginning-of-line)
  529.     )
  530.   )
  531.  
  532.  
  533. (defun hif-looking-at-ifX ()        ;; Should eventually see #if
  534.   (looking-at hif-ifx-regexp))
  535. (defun hif-looking-at-endif ()
  536.   (looking-at hif-endif-regexp))
  537. (defun hif-looking-at-else ()
  538.   (looking-at hif-else-regexp))
  539.  
  540.  
  541.  
  542. (defun hif-ifdef-to-endif ()
  543.   "If positioned at #ifX or #else form, skip to corresponding #endif."
  544. ;  (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
  545.   (hif-find-next-relevant)
  546.   (cond ((hif-looking-at-ifX)
  547.      (hif-ifdef-to-endif) ; find endif of nested if
  548.      (hif-ifdef-to-endif)) ; find outer endif or else
  549.     ((hif-looking-at-else)
  550.      (hif-ifdef-to-endif)) ; find endif following else
  551.     ((hif-looking-at-endif)
  552.      'done)
  553.     (t
  554.      (error "Missmatched #ifdef #endif pair"))
  555.     ))
  556.  
  557.  
  558. (defun hif-endif-to-ifdef ()
  559.   "If positioned at #endif form, skip backward to corresponding #ifX."
  560. ;  (message "hif-endif-to-ifdef at %d" (point))
  561.   (let ((start (point)))
  562.     (hif-find-previous-relevant)
  563.     (if (= start (point))
  564.     (error "Missmatched #ifdef #endif pair")))
  565.   (cond ((hif-looking-at-endif)
  566.      (hif-endif-to-ifdef) ; find beginning of nested if
  567.      (hif-endif-to-ifdef)) ; find beginning of outer if or else
  568.     ((hif-looking-at-else)
  569.      (hif-endif-to-ifdef))
  570.     ((hif-looking-at-ifX)
  571.      'done)
  572.     (t ; never gets here
  573.      )))
  574.  
  575.  
  576. (defun forward-ifdef (&optional arg)
  577.   "Move point to beginning of line of the next ifdef-endif.
  578.        With argument, do this that many times."
  579.   (interactive "p")
  580.   (or arg (setq arg 1))
  581.   (if (< arg 0)
  582.       (backward-ifdef (- arg)))
  583.   (while (< 0 arg)
  584.     (setq arg (- arg))
  585.     (let ((start (point)))
  586.       (if (not (hif-looking-at-ifX))
  587.       (hif-find-next-relevant))
  588.       (if (hif-looking-at-ifX)
  589.       (hif-ifdef-to-endif)
  590.     (goto-char start)
  591.     (error "No following #ifdef")
  592.     ))))
  593.  
  594.  
  595. (defun backward-ifdef (&optional arg)
  596.   "Move point to beginning of the previous ifdef-endif.
  597.        With argument, do this that many times."
  598.   (interactive "p")
  599.   (or arg (setq arg 1))
  600.   (if (< arg 0)
  601.       (forward-ifdef (- arg)))
  602.   (while (< 0 arg)
  603.     (setq arg (1- arg))
  604.     (beginning-of-line)
  605.     (let ((start (point)))
  606.       (if (not (hif-looking-at-endif))
  607.       (hif-find-previous-relevant))
  608.       (if (hif-looking-at-endif)
  609.       (hif-endif-to-ifdef)
  610.     (goto-char start)
  611.     (error "No previous #ifdef")
  612.     ))))
  613.  
  614.  
  615.  
  616. (defun down-ifdef ()
  617.   "Move point to beginning of nested ifdef or else-part."
  618.     (interactive)
  619.     (let ((start (point)))
  620.       (hif-find-next-relevant)
  621.       (if (or (hif-looking-at-ifX) (hif-looking-at-else))
  622.       ()
  623.     (goto-char start)
  624.     (error "No following #ifdef")
  625.     )))
  626.  
  627.  
  628. (defun up-ifdef ()
  629.   "Move point to beginning of enclosing ifdef or else-part."
  630.   (interactive)
  631.   (beginning-of-line)
  632.   (let ((start (point)))
  633.     (if (not (hif-looking-at-endif))
  634.     (hif-find-previous-relevant))
  635.     (if (hif-looking-at-endif)
  636.     (hif-endif-to-ifdef))
  637.       (if (= start (point))
  638.       (error "No previous #ifdef")
  639.     )))
  640.  
  641. (defun next-ifdef (&optional arg)
  642.   "Move to the beginning of the next #ifX, #else, or #endif.
  643.        With argument, do this that many times."
  644.   (interactive "p")
  645.   (or arg (setq arg 1))
  646.   (if (< arg 0)
  647.       (previous-ifdef (- arg)))
  648.   (while (< 0 arg)
  649.     (setq arg (1- arg))
  650.     (hif-find-next-relevant)
  651.     (if (eolp)
  652.     (progn
  653.       (beginning-of-line)
  654.       (error "No following #ifdefs, #elses, or #endifs")
  655.       ))))
  656.  
  657. (defun previous-ifdef (&optional arg)
  658.   "Move to the beginning of the previous #ifX, #else, or #endif.
  659.        With argument, do this that many times."
  660.   (interactive "p")
  661.   (or arg (setq arg 1))
  662.   (if (< arg 0)
  663.       (next-ifdef (- arg)))
  664.   (while (< 0 arg)
  665.     (setq arg (1- arg))
  666.     (let ((start (point)))
  667.       (hif-find-previous-relevant)
  668.       (if (= start (point))
  669.       (error "No previous #ifdefs, #elses, or #endifs")
  670.     ))))
  671.  
  672.  
  673. ;===%%SF%% parsing (End)  ===
  674.  
  675.  
  676. ;===%%SF%% hide-ifdef-hiding (Start)  ===
  677.  
  678.  
  679. ;;; A range is a structure with four components:
  680. ;;; ELSE-P    True if there was an else clause for the ifdef.
  681. ;;; START    The start of the range. (beginning of line)
  682. ;;; ELSE    The else marker (beginning of line)
  683. ;;;            Only valid if ELSE-P is true.
  684. ;;; END        The end of the range.  (beginning of line)
  685.  
  686. (defun hif-make-range (else-p start end &optional else)
  687.   (list else-p start else end))
  688.  
  689. (defun hif-range-else-p (range)  (elt range 0))
  690. (defun hif-range-start (range) (elt range 1))
  691. (defun hif-range-else (range) (elt range 2))
  692. (defun hif-range-end (range) (elt range 3))
  693.  
  694.  
  695.  
  696. ;;; Find-Range
  697. ;;; The workhorse, it delimits the #if region.  Reasonably simple:
  698. ;;; Skip until an #else or #endif is found, remembering positions.  If
  699. ;;; an #else was found, skip some more, looking for the true #endif.
  700.  
  701. (defun hif-find-range ()
  702.   "Returns a Range structure describing the current #if region.
  703. Point is left unchanged."
  704. ;  (message "hif-find-range at %d" (point))
  705.   (save-excursion
  706.     (beginning-of-line)
  707.     (let ((start (point))
  708.       (else-p nil)
  709.       (else nil)
  710.       (end nil))
  711.       ;; Part one.  Look for either #endif or #else.
  712.       ;; This loop-and-a-half dedicated to E. Dijkstra.
  713.       (hif-find-next-relevant)
  714.       (while (hif-looking-at-ifX)        ; Skip nested ifdef
  715.     (hif-ifdef-to-endif)
  716.     (hif-find-next-relevant))
  717.       ;; Found either a #else or an #endif.
  718.       (cond ((hif-looking-at-else)
  719.          (setq else-p t)
  720.          (setq else (point)))
  721.         (t
  722.          (setq end (point)) ; (save-excursion (end-of-line) (point))
  723.          ))
  724.       ;; If found #else, look for #endif.
  725.       (if else-p
  726.       (progn
  727.         (hif-find-next-relevant)
  728.         (while (hif-looking-at-ifX)    ; Skip nested ifdef
  729.           (hif-ifdef-to-endif)
  730.           (hif-find-next-relevant))
  731.         (if (hif-looking-at-else)
  732.         (error "Found two elses in a row?  Broken!"))
  733.         (setq end (point))  ; (save-excursion (end-of-line) (point))
  734.         ))
  735.       (hif-make-range else-p start end else))))
  736.  
  737.       
  738. ;;; A bit slimy.
  739. ;;; NOTE:  If there's an #ifdef at the beginning of the file, we can't
  740. ;;; hide it.  There's no previous newline to replace.  If we added
  741. ;;; one, we'd throw off all the counts.  Feh.
  742.  
  743. (defun hif-hide-line (point)
  744.   "Hide the line containing point.  Does nothing if
  745. hide-ifdef-lines is nil."
  746.   (if hide-ifdef-lines
  747.       (save-excursion
  748.     (goto-char point)
  749.     (let ((modp (buffer-modified-p)))
  750.       (unwind-protect
  751.           (progn
  752.         (beginning-of-line)
  753.         (if (not (= (point) 1))
  754.             (hide-ifdef-region (1- (point)) (point))))
  755.         (set-buffer-modified-p modp))
  756.       ))
  757.     ))
  758.           
  759.  
  760. ;;;  Hif-Possibly-Hide
  761. ;;;  There are four cases.  The #ifX expression is "taken" if it
  762. ;;;  the hide-ifdef-evaluator returns T.  Presumably, this means the code
  763. ;;;  inside the #ifdef would be included when the program was
  764. ;;;  compiled.  
  765. ;;;
  766. ;;;  Case 1:  #ifX taken, and there's an #else.
  767. ;;;    The #else part must be hidden.  The #if (then) part must be
  768. ;;;    processed for nested #ifX's.
  769. ;;;  Case 2:  #ifX taken, and there's no #else.
  770. ;;;    The #if part must be processed for nested #ifX's.
  771. ;;;  Case 3:  #ifX not taken, and there's an #else.
  772. ;;;    The #if part must be hidden.  The #else part must be processed
  773. ;;;    for nested #ifs.
  774. ;;;  Case 4:  #ifX not taken, and there's no #else.
  775. ;;;    The #ifX part must be hidden.
  776. ;;;
  777. ;;;  Further processing is done by narrowing to the relevant region
  778. ;;;  and just recursively calling hide-ifdef-guts.
  779. ;;;
  780. ;;;  When hif-possibly-hide returns, point is at the end of the
  781. ;;;  possibly-hidden range.
  782.  
  783. (defun hif-recurse-on (start end)
  784.   "Call hide-ifdef-guts after narrowing to end of START line and END
  785. line."
  786.   (save-excursion
  787.     (save-restriction
  788.       (goto-char start)
  789.       (end-of-line)
  790.       (narrow-to-region (point) end)
  791.       (hide-ifdef-guts))))
  792.  
  793. (defun hif-possibly-hide ()
  794.   "Called at #ifX expression, this hides those parts that should be
  795. hidden, according to judgement of hide-ifdef-evaluator."
  796. ;  (message "hif-possibly-hide") (sit-for 1)
  797.     (let ((test (hif-canonicalize))
  798.       (range (hif-find-range)))
  799. ;      (message "test = %s" test) (sit-for 1)
  800.       
  801.       (hif-hide-line (hif-range-end range))
  802.       (if (funcall hide-ifdef-evaluator test)
  803.       (cond ((hif-range-else-p range) ; case 1
  804.          (hif-hide-line (hif-range-else range))
  805.          (hide-ifdef-region (hif-range-else range) 
  806.                     (1- (hif-range-end range)))
  807.          (hif-recurse-on (hif-range-start range)
  808.                  (hif-range-else range)))
  809.         (t ; case 2
  810.          (hif-recurse-on (hif-range-start range)
  811.                  (hif-range-end range))))
  812.     (cond ((hif-range-else-p range) ; case 3
  813.            (hif-hide-line (hif-range-else range))
  814.            (hide-ifdef-region (hif-range-start range)
  815.                   (1- (hif-range-else range)))
  816.            (hif-recurse-on (hif-range-else range)
  817.                    (hif-range-end range)))
  818.           (t ; case 4
  819.            (hide-ifdef-region (point)
  820.                   (1- (hif-range-end range))))
  821.           ))
  822.       (hif-hide-line (hif-range-start range))    ; Always hide start.
  823.       (goto-char (hif-range-end range))
  824.       (end-of-line)
  825.       ))
  826.  
  827.  
  828.  
  829. (defun hide-ifdef-guts ()
  830.   "Does the work of hide-ifdefs, except for the work that's pointless
  831. to redo on a recursive entry."
  832. ;  (message "hide-ifdef-guts")
  833.   (save-excursion
  834.     (goto-char (point-min))
  835.     (while (hif-find-any-ifX)
  836.       (hif-possibly-hide))))
  837.  
  838. ;===%%SF%% hide-ifdef-hiding (End)  ===
  839.  
  840.  
  841. ;===%%SF%% exports (Start)  ===
  842.  
  843. (defvar hide-ifdef-initially nil
  844.   "*Non-nil if hide-ifdefs should be called when hide-ifdef-mode
  845.     is first activated.")
  846.  
  847. (defvar hide-ifdef-hiding nil
  848.   "Non-nil if text might be hidden.")
  849.  
  850. (defvar hide-ifdef-read-only nil
  851.   "*Set to non-nil if you want buffer to be read-only while hiding text.")
  852.  
  853. (defvar hif-outside-read-only nil
  854.   "Internal variable.  Saves the value of buffer-read-only while hiding.")
  855.  
  856. (defvar hide-ifdef-lines nil
  857.   "*Set to t if you don't want to see the #ifX, #else, and #endif lines.")
  858.  
  859. (defun hide-ifdef-toggle-read-only ()
  860.   "Toggle hide-ifdef-read-only."
  861.   (interactive)
  862.   (setq hide-ifdef-read-only (not hide-ifdef-read-only))
  863.   (message "Hide-Read-Only %s"
  864.        (if hide-ifdef-read-only "ON" "OFF"))
  865.   (if hide-ifdef-hiding
  866.       (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
  867.   (hif-update-mode-line)
  868.   )
  869.  
  870. (defun hide-ifdef-toggle-outside-read-only ()
  871.   "Replacement for toggle-read-only within hide-ifdef-mode."
  872.   (interactive)
  873.   (setq hif-outside-read-only (not hif-outside-read-only))
  874.   (message "Read only %s"
  875.        (if hif-outside-read-only "ON" "OFF"))
  876.   (setq buffer-read-only
  877.     (or (and hide-ifdef-hiding hide-ifdef-read-only)
  878.         hif-outside-read-only)
  879.     )
  880.   (hif-update-mode-line)
  881.   )
  882.  
  883.       
  884. (defun hide-ifdef-define (var)
  885.   "Define a VAR so that #ifdef VAR would be included."
  886.   (interactive "SDefine what? ")
  887.   (hif-set-var var t)
  888.   (if hide-ifdef-hiding (hide-ifdefs)))
  889.  
  890. (defun hide-ifdef-undef (var)
  891.   "Undefine a VAR so that #ifdef VAR would not be included."
  892.   (interactive "SUndefine what? ")
  893.   (hif-set-var var nil)
  894.   (if hide-ifdef-hiding (hide-ifdefs)))
  895.  
  896.  
  897. (defun hide-ifdefs ()
  898.   "Hide the contents of some #ifdefs.  Assume that defined symbols have
  899. been added to hide-ifdef-env.  The text hidden is the text that would not
  900. be included by the C preprocessor if it were given the file with those
  901. symbols defined.
  902.  
  903. Turn off hiding by calling show-ifdef."
  904.  
  905.   (interactive)
  906.   (message "Hiding...")
  907.   (if (not hide-ifdef-mode)
  908.       (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
  909.   (if hide-ifdef-hiding
  910.       (show-ifdefs))            ; Otherwise, deep confusion.
  911.   (if buffer-read-only (toggle-read-only)) ; make it writable temporarily
  912.   (setq selective-display t)
  913.   (setq hide-ifdef-hiding t)
  914.   (hide-ifdef-guts)
  915.   (if (or hide-ifdef-read-only hif-outside-read-only)
  916.       (toggle-read-only) ; make it read only
  917.     )
  918.   (message "Hiding done")
  919.   )
  920.  
  921.  
  922. (defun show-ifdefs ()
  923.   "Cancel the effects of hide-ifdef.  The contents of all #ifdefs is shown."
  924.   (interactive)
  925.   (if buffer-read-only (toggle-read-only)) ; make it writable temporarily
  926.   (setq selective-display nil)    ; defaults
  927.   (hif-show-all)
  928.   (if hif-outside-read-only
  929.       (toggle-read-only)) ; make it read only
  930.   (setq hide-ifdef-hiding nil)
  931.   )
  932.  
  933.  
  934. (defun hif-find-ifdef-block ()
  935.   "Utilitiy for hide and show ifdef-block.  Set top and bottom of ifdef block."
  936.   (let (max-bottom)
  937.   (save-excursion
  938.     (beginning-of-line)
  939.     (if (not (or (hif-looking-at-else) (hif-looking-at-ifX)))
  940.     (up-ifdef))
  941.     (setq top (point))
  942.     (hif-ifdef-to-endif)
  943.     (setq max-bottom (1- (point)))
  944.     )
  945.   (save-excursion
  946.     (beginning-of-line)
  947.     (if (not (hif-looking-at-endif))
  948.     (hif-find-next-relevant))
  949.     (while (hif-looking-at-ifX)
  950.       (hif-ifdef-to-endif)
  951.       (hif-find-next-relevant)
  952.       )
  953.     (setq bottom (min max-bottom (1- (point))))
  954.     ))
  955.   )
  956.  
  957.  
  958. (defun hide-ifdef-block ()
  959.   "Hide the ifdef block (true or false part) enclosing or before the cursor."
  960.   (interactive)
  961.   (if (not hide-ifdef-mode)
  962.       (hide-ifdef-mode 1))
  963.   (if buffer-read-only (toggle-read-only))
  964.   (setq selective-display t)
  965.   (let (top bottom)
  966.     (hif-find-ifdef-block) ; set top and bottom - dynamic scoping
  967.     (hide-ifdef-region top bottom)
  968.     (if hide-ifdef-lines
  969.     (progn
  970.       (hif-hide-line top)
  971.       (hif-hide-line (1+ bottom))))
  972.     (setq hide-ifdef-hiding t)
  973.     )
  974.   (if (or hide-ifdef-read-only hif-outside-read-only)
  975.       (toggle-read-only))
  976.   )
  977.  
  978.  
  979. (defun show-ifdef-block ()
  980.   "Show the ifdef block (true or false part) enclosing or before the cursor."
  981.   (interactive)
  982.   (let ((old-read-only buffer-read-only))
  983.     (if old-read-only (toggle-read-only))
  984.     (if hide-ifdef-lines
  985.     (save-excursion
  986.       (beginning-of-line)
  987.       (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point))))
  988.  
  989.       (let (top bottom)
  990.     (hif-find-ifdef-block)
  991.     (hif-show-ifdef-region (1- top) bottom))
  992.       )
  993.  
  994.     ; restore read only status since we dont know if all is shown.
  995.     (if old-read-only (toggle-read-only))
  996.     ))
  997.  
  998.  
  999.  
  1000. ;;;  defininition alist support
  1001.  
  1002. (defvar hide-ifdef-define-alist nil
  1003.   "A global assoc list of pre-defined symbol lists")
  1004.  
  1005. (defun hif-compress-define-list (env)
  1006.   "Compress the define list ENV into a list of defined symbols only."
  1007.   (let ((defs (mapcar '(lambda (arg)
  1008.              (if (hif-lookup (car arg)) (car arg)))
  1009.               env))
  1010.     (new-defs nil))
  1011.     (while defs
  1012.       (if (car defs)
  1013.       (setq new-defs (cons (car defs) new-defs)))
  1014.       (setq defs (cdr defs)))
  1015.     new-defs
  1016.     ))
  1017.  
  1018. (defun hide-ifdef-set-define-alist (name)
  1019.   "Set the association for NAME to hide-ifdef-env."
  1020.   (interactive "SSet define list: ")
  1021.   (setq hide-ifdef-define-alist
  1022.     (cons (cons name (hif-compress-define-list hide-ifdef-env))
  1023.           hide-ifdef-define-alist))
  1024.   )
  1025.  
  1026. (defun hide-ifdef-use-define-alist (name)
  1027.   "Set hide-ifdef-env to the define list specified by NAME."
  1028.   (interactive "SUse define list: ")
  1029.   (let ((define-list (assoc name hide-ifdef-define-alist)))
  1030.     (if define-list
  1031.     (setq hide-ifdef-env
  1032.           (mapcar '(lambda (arg) (cons arg t))
  1033.               (cdr define-list)))
  1034.       (error "No define list for %s" name))
  1035.     (if hide-ifdef-hiding (hide-ifdefs))
  1036.     )
  1037.   )
  1038.  
  1039. ;===%%SF%% exports (End)  ===
  1040.